package org.fh.inductionLamp.impl.response;

import org.fh.HandleData;
import org.fh.common.ResponseCode;
import org.fh.domain.DataFrame;
import org.fh.inductionLamp.api.LampResponse;
import org.fh.utils.ConvertCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

/**
 * @Author: wwy
 * @Date: 2020/10/22 16:21
 */
public class ModeSwitchResponse implements LampResponse {
    private static final Logger LOGGER = LoggerFactory.getLogger(ModeSwitchResponse.class);
    private boolean success;
    private String code;
    private String data;
    public ModeSwitchResponse(String destId){
        HashMap<String, DataFrame> tcpDataHashMap = HandleData.tcpDataHashMap;
        int count = 0;

        while (true){
            if(count == 30){
                this.code = ResponseCode.TIME_OUT;
                this.success = false;
                break;
            }
            DataFrame dataFrame = tcpDataHashMap.get(destId);
            if(dataFrame != null){
                if(dataFrame.getDataStr() != null && !"".equals(dataFrame.getDataStr())){
                    this.code = ResponseCode.SUCCESS;
                    this.success = true;
                    this.data = dataFrame.getDataStr();
                    this.dataHandle(data);
                    //删除该条数据
                    tcpDataHashMap.remove(destId);
                    break;
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                LOGGER.info("线程异常{}",e.getMessage());
            }
            count ++;
        }
    }

    /**
     * 数据处理
     * @param data
     */
    public void dataHandle(String data){
        String one = data.substring(0, 2);
        if("AA".equalsIgnoreCase(one) || "55".equalsIgnoreCase(one)){
            //切换成功
            LOGGER.info("模式切换成功");
        }
    }
    @Override
    public boolean isSuccess() {
        return this.success;
    }

    @Override
    public String code() {
        return this.code;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ModeSwitchResponse{" +
                "success=" + success +
                ", code='" + code + '\'' +
                ", data='" + data + '\'' +
                '}';
    }
}
